package MatchReport;
use base Module;
use strict;
use utf8;
use LWP::Simple;

my $lastreport = "";
my $lastidx = 0;

sub fromhtml {
    my $bold = chr(2);
    $_[0] =~ s/<b>(.*?)<\/b>/$bold $1 $bold/gx;
    return $_[0];
}

sub check_raport
{
    my $text = get("http://www.sport24.pl/pilka/live/show/2263/");
    my $fromidx = $lastidx;
    my @reports = ($text =~ /<div style=\".*?background.*?\".*?>(.*?)<\/div>/g);
    my $toidx = $#reports;
    
    if($fromidx == 0) {
	$fromidx = $toidx - 12;
    }
    $lastidx = $toidx;
    print "$fromidx to $toidx\n";


    map {fromhtml($_)} @reports;
    map {$_ =~ s/<(.*?)>//g} @reports;
    
    
    my @newreports;
    foreach my $report ((reverse @reports)[$fromidx .. $toidx]) {
	if($report eq $lastreport) {
	    last;
	}
	Core::send_msg($report);
	$lastreport = $report;
    }
}

sub on_load {
    Timer::set(\&check_raport, 5, 1);
}
1;